package com.qfedu.util;

import io.jsonwebtoken.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class TokenUtil {

    public static String createToken(String userInfo,String uid){
        String token = Jwts.builder()
                .setSubject(userInfo)     //设置用户信息
                .setId(uid)             //设置用ID
                .setIssuedAt(new Date())            //设置token的创建时间
                .setExpiration(new Date(System.currentTimeMillis()+24*60*60*1000))  //设置过期时间
                .signWith(SignatureAlgorithm.HS256,"qianfeng")  //加密方式及key
                .compact();
        return token;
    }


    public static Map<String,String> parserToken(String token) throws Exception{
        JwtParser parser = Jwts.parser();
        parser.setSigningKey("qianfeng");
        Jws<Claims> claimsJws = parser.parseClaimsJws(token);
        Claims body = claimsJws.getBody();
        Map<String, String> map = new HashMap<String, String>();
        map.put("uid",body.getId());
        map.put("userInfo",body.getSubject());
        return map;
    }
}
